package com.supor.aiot.common.base;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.view.View;

import com.android.baseconfig.common.helper.NetObserverManager;
import com.android.baseconfig.common.utils.Logc;
import com.android.baseconfig.common.utils.ToastUtils;
import com.android.baseconfig.common.views.AppBasicDialog;
import com.supor.aiot.common.R;
import com.supor.aiot.common.app.Constants;
import com.supor.aiot.common.helper.ConfigActivityManager;

import androidx.annotation.RequiresApi;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.AndroidViewModel;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

/**
 * ---------------------------------------------------------------- <br>
 * Copyright (C) 2014-2020, by het, Shenzhen, All rights reserved.  <br>
 * ---------------------------------------------------------------- <br>
 * <p>
 * 描述: 配网基类 <br>
 * 作者: lei <br>
 * 日期: 2020/12/3 <br>
 */
public abstract class BaseConfigActivity<VM extends AndroidViewModel, SV extends ViewDataBinding> extends BaseMVVMActivity<VM, SV> {
    // @BindView(R2.id.toolbar)
    // protected CustomTitleBar toolbar;
    protected View view;
    protected static final String TAG = BaseConfigActivity.class.getSimpleName();
    private AppBasicDialog commonDialog;
    private AppBroadcastReceiver appBroadcastReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setAndroidNativeLightStatusBar(this, true);
        ConfigActivityManager.addActivity(this);
        appBroadcastReceiver = new AppBroadcastReceiver();
    }

    // @Override
    // protected BasePresenter createPresenter() {
    //     return null;
    // }

    /**
     * 是否改变状态栏文字颜色为黑色，默认为黑色
     */
    protected boolean isUseBlackFontWithStatusBar() {
        return true;
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    protected static void setAndroidNativeLightStatusBar(Activity activity, boolean dark) {
        View decor = activity.getWindow().getDecorView();
        if (dark) {
            decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        } else {
            decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        }
    }

    public void tips(final String msg) {
        this.runOnUiThread(() -> {
            ToastUtils.getInstance().showCustomToast(msg);
        });
    }

    protected void showDialog(String title, String msg, String yesText,
                              DialogInterface.OnClickListener callback, String noText,
                              DialogInterface.OnClickListener noCallback) {
        if (commonDialog != null && commonDialog.isShowing()) {
            commonDialog.dismiss();
        }
        AppBasicDialog.Builder builder = new AppBasicDialog.Builder(this, false);
        builder.setTitle(title);
        builder.setMessage(msg);
        builder.setNegativeButton(noText, noCallback);
        builder.setPositiveButton(yesText, callback);
        commonDialog = builder.create();
        commonDialog.setCancelable(false);
        commonDialog.show();
    }

    protected void showDialog(String title, String msg, String yesText,
                              DialogInterface.OnClickListener callback) {
        if (commonDialog != null && commonDialog.isShowing()) {
            commonDialog.dismiss();
        }
        AppBasicDialog.Builder builder = new AppBasicDialog.Builder(this, false);
        builder.setTitle(title);
        builder.setMessage(msg);
        builder.setPositiveButton(yesText, callback);
        commonDialog = builder.create();
        commonDialog.setCancelable(false);
        commonDialog.show();
    }

    protected void closeDialog() {
        if (commonDialog != null && commonDialog.isShowing()) {
            commonDialog.dismiss();
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onPause() {
        super.onPause();
        LocalBroadcastManager.getInstance(this).unregisterReceiver(appBroadcastReceiver);
    }

    @Override
    protected void onResume() {
        super.onResume();
        LocalBroadcastManager.getInstance(this).registerReceiver(appBroadcastReceiver,
                new IntentFilter(Constants.PUSH_SERVICE_ACTION));
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    public void onDestroy() {
        ConfigActivityManager.removeActivity(this);
        NetObserverManager.getInstance().removeNetObserver(this);
        super.onDestroy();
    }

    @Override
    public void updateNetStatus(int type) {

    }

    @Override
    public void updateNFCStatus(int state) {

    }

    @Override
    public void updateWifiStatus(int state) {

    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        overridePendingTransition(R.anim.activity_open_enter, R.anim.activity_open_exit);
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        super.startActivityForResult(intent, requestCode);
        overridePendingTransition(R.anim.activity_open_enter, R.anim.activity_open_exit);
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.activity_close_enter, R.anim.activity_close_exit);
    }

    final class AppBroadcastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent != null && Constants.PUSH_SERVICE_ACTION.equals(intent.getAction())) {
                Logc.d("onReceive", "push: " + Constants.PUSH_SERVICE_ACTION);
                ConfigActivityManager.finishAll();
            }
        }
    }
}
